package com.zg.autoform.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoform.service.FormService;
import com.zg.autoform.web.req.FormTablePageReq;
import com.zg.common.core.dao.autoform.DiyColumn;
import com.zg.common.core.dao.autoform.DiyColumnDao;
import com.zg.common.core.dao.autoform.DiyForm;
import com.zg.common.core.dao.autoform.DiyFormDao;
import com.zg.common.core.req.PageReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * DESCRIPTION
 *
 * @author zhanfeng.liang
 * @since 2025-07-17
 */
@Service
@Slf4j
public class FormServiceImpl implements FormService {


    @Autowired
    private  DiyFormDao formDao;

    @Autowired
    private DiyColumnDao columnDao;

    @Override
    public Page<Map<String,Object>> pageTable(FormTablePageReq  req ){
        Page<Map<String,Object>> page = new Page<>(req.getPage(), req.getPageSize());
        Page<Map<String,Object>> pageInfo = formDao.pageTable(page, req.getTableName());

        List<Map<String,Object>> voList = pageInfo.getRecords().stream()
                .collect(Collectors.toList());
        Page<Map<String,Object>> voPage = new Page<>(pageInfo.getCurrent(), pageInfo.getSize(), pageInfo.getTotal());
        voPage.setRecords(voList);
        return voPage;
    }

    public DiyForm getByCode(String code){
        LambdaQueryWrapper<DiyForm> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DiyForm::getCode, code);
        return formDao.getOne(lambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void importFromTable(String tableName){
        LambdaQueryWrapper<DiyForm> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DiyForm::getCode, tableName);
        DiyForm form = formDao.getOne(lambdaQueryWrapper);
        if(form!=null){
            LambdaQueryWrapper<DiyColumn> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(DiyColumn::getFormId , form.getId());
            columnDao.remove(queryWrapper);
            formDao.removeById(form.getId());
        }

        String comments = formDao.getComments(tableName);
        List<Map<String,Object>> list = columnDao.listColumn(tableName);
        DiyForm diyForm = new DiyForm();
        diyForm.setName(comments);
        diyForm.setCode(tableName.toLowerCase(Locale.ROOT));
        formDao.getBaseMapper().insert(diyForm);
        for (Map<String,Object> map : list) {
            DiyColumn column = new DiyColumn();
            String columnName=map.get("COLUMN_NAME").toString().toLowerCase(Locale.ROOT);
            column.setCode(columnName);

            if(map.get("COMMENTS")==null){
                column.setName(columnName);
            }else {
                column.setName(map.get("COMMENTS").toString());
            }
            column.setDataType(map.get("DATA_TYPE").toString());
            column.setFormId(diyForm.getId());
            columnDao.getBaseMapper().insert(column);
        }
    }
}
